/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MOVEMENT_MANAGER_H
#define MOVEMENT_MANAGER_H

#include <set>
#include <map>

#include "sensor_if.h"

#include "movement_data_utils.h"
#include "imovement_algorithm.h"
#include "imovement_callback.h"
#include "movement_common.h"
#include "movement_msdp_client_impl.h"
#include "rdb_store.h"
#include "rdb_helper.h"
#include "rdb_open_callback.h"
#include "rdb_store_config.h"

namespace OHOS {
namespace Msdp {
class MovementService;
class MovementManager {
public:
    explicit MovementManager(const wptr<MovementService>& ms) : ms_(ms)
    {
        MOVEMENT_HILOGI(MOVEMENT_MODULE_SERVICE, "MovementManager instance is created.");
    }
    ~MovementManager() = default;

    class MovementCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
    public:
        MovementCallbackDeathRecipient() = default;
        virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
        virtual ~MovementCallbackDeathRecipient() = default;
    };

    bool Init();
    bool InitInterface();
    bool InitDataCallback();
    void NotifyMovementChange(const MovementDataUtils::MovementData& movementData);
    void Subscribe(const MovementDataUtils::MovementType& type, const sptr<ImovementCallback>& callback);
    void UnSubscribe(const MovementDataUtils::MovementType& type, const sptr<ImovementCallback>& callback);
    MovementDataUtils::MovementData GetLatestMovementData(const MovementDataUtils::MovementType& type);
    int32_t SensorDataCallback(const struct SensorEvents *event);
    int32_t MsdpDataCallback(MovementDataUtils::MovementData& data);
    int32_t WriteObserverDataToDatabase(MovementDataUtils::MovementData& data);
    int32_t LoadAlgorithm(bool bCreate);
    int32_t UnloadAlgorithm(bool bCreate);
private:
    struct classcomp {
        bool operator()(const sptr<ImovementCallback> &l, const sptr<ImovementCallback> &r) const
        {
            return l->AsObject() < r->AsObject();
        }
    };
    const wptr<MovementService> ms_;
    std::mutex mutex_;
    sptr<IRemoteObject::DeathRecipient> movementCBDeathRecipient_;
    std::unique_ptr<MovementMsdpClientImpl> msdpImpl_;
    std::shared_ptr<NativeRdb::RdbStore> store_;
    int64_t primaryKey_ = 1;
    std::map<MovementDataUtils::MovementType, MovementDataUtils::MovementValue> msdpData_;
    std::map<MovementDataUtils::MovementType, std::set<const sptr<ImovementCallback>, classcomp>> listenerMap_;
};

class InsertCallback : public NativeRdb::RdbOpenCallback {
public:
    int OnCreate(NativeRdb::RdbStore &rdbStore) override;
    int OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
};
} // namespace Msdp
} // namespace OHOS
#endif // MOVEMENT_MANAGER_H
